package com.king.modules.monitor.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.annotation.Log;
import com.king.common.constant.CacheConstants;
import com.king.common.enums.BusinessType;
import com.king.common.utils.redis.RedisUtil;
import com.king.common.utils.text.StringUtils;
import com.king.framework.base.controller.BaseController;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.framework.base.domain.vo.LoginUser;
import com.king.framework.base.domain.vo.PageResult;
import com.king.modules.monitor.service.SysUserOnlineService;
import com.king.modules.system.domain.vo.SysUserOnline;

/**
 * 在线用户监控
 * 
 * @author king
 */
@RestController
@RequestMapping("/monitor/online")
public class SysOnlineController extends BaseController {
	@Autowired
	private SysUserOnlineService userOnlineService;

	@Autowired
	private RedisUtil redisCache;

//	@PreAuthorize("@sps.hasAuthority('monitor:online:query')")
//	@GetMapping("/list")
//	public TableDataInfo list(String ipaddr, String userName) {
//		Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
//		List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
//		for (String key : keys) {
//			LoginUser user = redisCache.getCacheObject(key);
//			if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
//				if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
////					userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
//					userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//				}
//			} else if (StringUtils.isNotEmpty(ipaddr)) {
//				if (StringUtils.equals(ipaddr, user.getIpaddr())) {
////					userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
//					userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//				}
//			} else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
//				if (StringUtils.equals(userName, user.getUsername())) {
////					userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
//					userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//				}
//			} else {
//				userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//			}
//		}
//		Collections.reverse(userOnlineList);
//		userOnlineList.removeAll(Collections.singleton(null));
//		return getDataTable(userOnlineList);
//	}
	

	@PreAuthorize("@sps.hasAuthority('monitor:online:query')")
	@PostMapping("/query")
	public AjaxResult query(@RequestBody SysUserOnline form) {
		long total = 0L;
		Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
		List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
		for (String key : keys) {
			LoginUser user = redisCache.getCacheObject(key);
			if (StringUtils.isNotEmpty(form.getIpaddr()) && StringUtils.isNotEmpty(form.getUserName())) {
				if (StringUtils.equals(form.getIpaddr(), user.getIpaddr()) && StringUtils.equals(form.getUserName(), user.getUsername())) {
//					userOnlineList.add(userOnlineService.selectOnlineByInfo(form.getIpaddr(), form.getUserName(), user));
					userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
				}
			} else if (StringUtils.isNotEmpty(form.getIpaddr())) {
				if (StringUtils.equals(form.getIpaddr(), user.getIpaddr())) {
//					userOnlineList.add(userOnlineService.selectOnlineByIpaddr(form.getIpaddr(), user));
					userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
				}
			} else if (StringUtils.isNotEmpty(form.getUserName()) && StringUtils.isNotNull(user.getUser())) {
				if (StringUtils.equals(form.getUserName(), user.getUsername())) {
//					userOnlineList.add(userOnlineService.selectOnlineByUserName(form.getUserName(), user));
					userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
				}
			} else {
				userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
			}
		}
		total = userOnlineList.size();
		Collections.reverse(userOnlineList);
		userOnlineList.removeAll(Collections.singleton(null));
		
		PageResult<SysUserOnline> datas = new PageResult<SysUserOnline>(total, userOnlineList);
	    return success(datas);
	}

	/**
	 * 强退用户
	 */
	@PreAuthorize("@sps.hasAuthority('monitor:online:forceLogout')")
	@Log(title = "在线用户", businessType = BusinessType.FORCE)
	@PostMapping("/{tokenId}")
	public AjaxResult forceLogout(@PathVariable String tokenId) {
		redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
		return success();
	}
}
